package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivitySelPicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    private SelPictureAdapter pictureAdapter;
    public static List<SelectMediaEntity> selList=new ArrayList<>();
    private List<SelectMediaEntity> listShow=new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelPicBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext,3));
        pictureAdapter=new SelPictureAdapter();
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        mDataBinding.tvSelPic.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvSelPic:
                List<String> listPath=new ArrayList<>();
                for (SelectMediaEntity selectMediaEntity : selList) {
                    if (!TextUtils.isEmpty(selectMediaEntity.getPath())){
                        listPath.add(selectMediaEntity.getPath());
                    }
                }
                if (listPath.size()==0){
                    ToastUtils.showShort(getString(R.string.please_sel_picture));
                    return;
                }
                PicConvertActivity.listPath=listPath;
                startActivity(PicConvertActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (pictureAdapter.getItem(position).isChecked()) {
            pictureAdapter.getItem(position).setChecked(false);
            selList.remove(pictureAdapter.getItem(position));
            pictureAdapter.notifyDataSetChanged();
            mDataBinding.tvSelPic.setText(getString(R.string.sel_pic)+"（"+selList.size()+"/9）");
        } else {
            if (selList.size() >= 9) {
                ToastUtils.showShort(getString(R.string.sel_pic_tip));
            }{
                selList.add(pictureAdapter.getItem(position));
            }
            mDataBinding.tvSelPic.setText(getString(R.string.sel_pic)+"（"+selList.size()+"/9）");
            pictureAdapter.getItem(position).setChecked(true);
            pictureAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initData() {
        getPermission();
        selList.clear();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPicList.setVisibility(View.GONE);
                        mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listShow.addAll(selectMediaEntities);
                    pictureAdapter.setList(listShow);
                    mDataBinding.rvPicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPicList.setVisibility(View.GONE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}